<template>
  <!-- 设备管理 -->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="摄像头名称" prop="cameraName">
        <el-input v-model="queryParams.cameraName"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="!selectedRows.length"
          @click="handleBatchDelete"
          v-hasPermi="['system:notice:remove']"
          >删除</el-button
        >
      </el-col>
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-hasPermi="['system:notice:export']"
        >导出</el-button
      >
    </el-row>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      :height="500"
      style="width: 100%"
      :default-sort="{ prop: 'date', order: 'descending' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />

      <el-table-column
        prop="imgGeo"
        label="图像定位位置"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="imgGeoNum"
        label="图像定位返回个数"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="cameraName"
        label="摄像头名称"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="imgUrl"
        label="图像地址"
        show-overflow-tooltip
        sortable
      >
        <template slot-scope="scope">
          <el-image
            :src=compImgUrl(scope.row.imgUrl)
            style="width: 100px; height: 100px"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="imgName"
        label="图像名称"
        show-overflow-tooltip
        sortable
      />

      <el-table-column
        prop="operation"
        label="操作"
        width="250"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPageChange"
      :current-page="queryParams.pageNum"
      :page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px; text-align: center"
    >
    </el-pagination>
    <!-- 删除弹窗 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确认删除这些任务吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 新增编辑 -->

    <el-dialog
      :title="title + '图像定位'"
      :visible.sync="addDialogVisibleNew"
      width="50%"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <div class="apk-detail-content">
        <el-card class="box-card">
          <el-form
            ref="formCameraGeoNew"
            :model="formCameraGeoNew"
            :rules="rules"
            label-width="140px"
          >
            <el-row>
              <el-form-item label="定位方式" prop="imgGeoType">
                <el-select
                  @change="handleChange"
                  v-model="formCameraGeoNew.imgGeoType"
                  placeholder="请选择定位方式"
                >
                  <el-option label="图像定位" value="1"></el-option>
                  <el-option label="摄像头定位" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-row>
            <el-row v-show="formCameraGeoNew.imgGeoType == 2">
              <el-col :span="12">
                <el-form-item
                  :rules="[{ required: true, message: '请输入摄像头名称' }]"
                  label="摄像头名称"
                  prop="cameraId"
                >
                  <el-select
                    @change="handleCameraChange"
                    v-model="formCameraGeoNew.cameraId"
                    placeholder="请选择摄像头名称"
                  >
                    <el-option
                      v-for="item in cameraList"
                      :key="item.id"
                      :label="item.id + '-' + item.cameraIp"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <!-- 图片地址不可编辑 后面加个小眼睛可以预览 -->
                <el-form-item
                  v-show="showImageUrl != null && showImageUrl != ''"
                  label="图片地址"
                >
                  <div style="display: flex">
                    <el-input v-model="showImageUrl" disabled></el-input>
                    <el-button
                      style="margin-left: 10px"
                      type="text"
                      @click="handleImgDelete"
                      >删除</el-button
                    >
                  </div>
                </el-form-item>
                <el-form-item
                  v-show="showImageUrl == null || showImageUrl == ''"
                  label="图片上传"
                  prop="imgUrl"
                  required
                >
                  <div>
                    <!-- 图片上传只有一个图片 传完之后展示上面的预览 -->
                    <el-upload
                      :action="imgUploadUrl"
                      :headers="headers"
                      :show-file-list="false"
                      :on-success="handleUploadSuccess"
                      :on-error="handleUploadError"
                      :before-upload="beforeAvatarUpload"
                    >
                      <el-button type="text">点击上传</el-button>
                    </el-upload>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-show="formCameraGeoNew.imgGeoType == 2">
              <el-col :span="12">
                <el-form-item
                  :rules="[
                    { required: true, message: '请输入图像定位返回个数' },
                  ]"
                  label="图像定位返回个数"
                  prop="imgGeoNum"
                >
                  <el-input v-model="formCameraGeoNew.imgGeoNum"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <!-- 城市 -->
                <el-form-item
                  :rules="[{ required: true, message: '请输入城市' }]"
                  label="城市"
                  prop="imgGeo"
                >
                  <el-input v-model="formCameraGeoNew.imgGeo"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <!-- 请选择框图像定位的算法 -->
                <el-form-item
                  :rules="[{ required: true, message: '请选择图像定位的算法' }]"
                  label="图像定位的算法"
                  prop="methodId"
                >
                  <el-select
                    v-model="formCameraGeoNew.methodId"
                    placeholder="请选择图像定位的算法"
                  >
                    <el-option label="图像地理定位算法" value="16"></el-option>
                    <el-option label="图像街景定位算法" value="23"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row v-show="formCameraGeoNew.imgGeoType == 1">
              <!-- 图像定位的算法 -->
              <el-col :span="12">
                <el-form-item required label="图像定位的算法" prop="methodId">
                  <el-select
                    v-model="formCameraGeoNew.methodId"
                    placeholder="请选择图像定位的算法"
                  >
                    <el-option label="图像地理定位算法" value="16"></el-option>
                    <el-option label="图像街景定位算法" value="23"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <!-- 图片上传 -->
                <el-form-item
                  v-show="showImageUrl != null && showImageUrl != ''"
                  label="图片地址"
                >
                  <div style="display: flex">
                    <el-input v-model="showImageUrl" disabled></el-input>
                    <el-button
                      style="margin-left: 10px"
                      type="text"
                      @click="handleImgDelete"
                      >删除</el-button
                    >
                  </div>
                </el-form-item>
                <el-form-item
                  v-show="showImageUrl == null || showImageUrl == ''"
                  label="图片上传"
                  prop="imgUrl"
                >
                  <el-upload
                  :before-upload="beforeAvatarUpload"
                    :action="imgUploadUrl"
                    :headers="headers"
                    :show-file-list="false"
                    :on-success="handleUploadSuccess"
                    :on-error="handleUploadError"
                  >
                    <el-button type="text">点击上传</el-button>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-show="formCameraGeoNew.imgGeoType == 1">
              <el-col :span="12">
                <el-form-item
                  :rules="[{ required: true, message: '请输入城市' }]"
                  label="城市"
                  prop="imgGeo"
                >
                  <el-input v-model="formCameraGeoNew.imgGeo"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  :rules="[
                    { required: true, message: '请输入图像定位返回个数' },
                  ]"
                  label="图像定位返回个数"
                  prop="imgGeoNum"
                >
                  <el-input v-model="formCameraGeoNew.imgGeoNum"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :offset="12" :span="12">
                <el-form-item>
                  <el-button type="primary" @click="onSubmit" size="small"
                    >提交</el-button
                  >
                  <el-button size="small" @click="cancel">取消</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </el-dialog>
    <!-- 预览 -->
    <el-dialog :visible.sync="previewVisible" width="50%">
      <el-image :src="showImageUrl"></el-image>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

import { listInfo } from "@/api/system/info";
import {
  listCameraGeo,
  addCameraGeo,
  updateCameraGeo,
  delCameraGeo,
} from "@/api/positionAnalysis/imageLocalization";
export default {
  data() {
    return {
      showImageUrl: "",
      imgUploadUrl: process.env.VUE_APP_BASE_API + "/upload",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      dialogVisible: false,
      selectedRows: [], // 存放选中的行
      tableData: [],
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      addDialogVisible: false,
      addDialogVisibleNew: false,
      formCameraGeo: {},
      formCameraGeoNew: {
        imgGeoType: "1",
      },
      rules: {
        imgName: [
          { required: true, message: "请输入图像名称", trigger: "blur" },
        ],
        cameraName: [
          { required: true, message: "请输入摄像头名称", trigger: "blur" },
        ],
      },
      title: "",
      cameraList: [], //摄像头列表
      previewVisible: false, //预览
    };
  },
  methods: {
    compImgUrl(url) {
      if (url) {
        return `http://192.168.0.121:9000${url}`
      }
    },
    //格式限制 png jpeg bmp svg
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isBMP = file.type === "image/bmp";
      const isSVG = file.type === "image/svg+xml";
      if (!isJPG && !isPNG && !isBMP && !isSVG) {
        this.$message.error("上传头像图片只能是 JPG 或 PNG 或 BMP 或 SVG 格式!");
        return false;
      }
      return true;
    },
    // 详情
    handleDetail(row) {
      this.$router.push({
        path: "/imageLocalization/detail",
        query: {
          id: row.id,
        },
      });
    },
    // 关闭弹窗
    closeDialog() {
      this.formCameraGeoNew = {};
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      console.log(res, file);
      debugger;
      this.formCameraGeoNew.imgUrl = res.data.url;
      this.showImageUrl = "http://192.168.0.117:9000" + res.data.url;
    },
    // 上传失败
    handleUploadError(err, file) {
      this.$message({
        message: "上传失败",
        type: "error",
      });
    },
    // 删除
    handleRemove(file) {
      this.formCameraGeoNew.imgUrl = "";
    },
    // 预览
    handlePreview() {
      //地址为空提示不可预览
      if (!this.showImageUrl) {
        this.$message({
          message: "图片地址为空，不可预览",
          type: "warning",
        });
        return;
      }
      this.previewVisible = true;
    },
    // 摄像头名称改变
    handleCameraChange(val) {
      this.showImageUrl = "";
      let url = this.cameraList.find((item) => item.id == val).jpgViewUrl;
      this.formCameraGeoNew.imgUrl = url;

      if (url) {
        this.showImageUrl = "http://192.168.0.121:9000" + url;
        console.log("this.showImageUrl", this.showImageUrl);
      } else {
        this.showImageUrl = "";
      }
    },
    getCameraList() {
      listInfo({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.cameraList = res.rows || [];
      });
    },
    handleChange(val) {},
    // 编辑
    handleEdit(row) {
      this.title = "编辑";
      this.addDialogVisibleNew = true;
      this.formCameraGeo = JSON.parse(JSON.stringify(row));
    },
    // 取消
    cancel() {
      this.resetForm("formCameraGeo");
      this.addDialogVisibleNew = false;
    },
    // 提交
    onSubmit() {
      if (this.title == "新增") {
        if (!this.formCameraGeoNew.imgUrl) {
          this.$message({
            message: "请输入图片",
            type: "warning",
          });
          return;
        }
        //调用之前手动检测如果是图片定位检测字段是否存在
        if (this.formCameraGeoNew.imgGeoType == 1) {
          //图片单独
          //图片  定位算法  城市 图像返回个数必填
          if (
            !this.formCameraGeoNew.imgUrl ||
            !this.formCameraGeoNew.methodId ||
            !this.formCameraGeoNew.imgGeo ||
            !this.formCameraGeoNew.imgGeoNum
          ) {
            this.$message({
              message: "请输入必填字段",
              type: "warning",
            });
            return;
          }
        }
        if (this.formCameraGeoNew.imgGeoType == 2) {
          //摄像头定位
          if (
            !this.formCameraGeoNew.cameraId ||
            !this.formCameraGeoNew.imgGeoNum ||
           !this.formCameraGeoNew.imgGeo
          ) {
            this.$message({
              message: "请输入必填字段",
            });
            return;
          }
        }
        addCameraGeo(this.formCameraGeoNew).then((res) => {
          if (res.code == 200) {
            this.getList();
            this.addDialogVisibleNew = false;
            this.$message({
              type: "success",
              message: "新增成功!",
            });
          }
        });
      } else {
        updateCameraGeo(this.formCameraGeo).then((res) => {
          if (res.code == 200) {
            this.getList();
            this.addDialogVisible = false;
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
          }
        });
      }
    },
    // 新增
    handleAdd() {
      this.resetForm("formCameraGeo");
      this.addDialogVisibleNew = true;
      this.title = "新增";
    },
    // 重置
    resetQuery() {
      this.resetForm("queryParams");
      this.getList();
    },
    // 检索
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    // 获取列表
    getList() {
      listCameraGeo(this.queryParams).then((res) => {
        this.tableData = res.rows || [];
        this.total = res.total;
      });
    },
    // 选中列表
    handleSelectionChange(rows) {
      this.selectedRows = rows;
    },
    // 每页个数
    handleSizeChange(newSize) {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = newSize;
      this.getList();
    },
    // 翻页
    handleCurrentPageChange(newPage) {
      this.queryParams.pageNum = newPage;
      this.getList();
    },
    // 批量删除
    handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message({
          message: "请至少选择一个设备进行删除",
          type: "warning",
        });
        return;
      }
      this.$confirm("确认删除这些设备吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let ids = [];
          this.selectedRows.map((ele) => {
            ids.push(ele.id);
          });
          delCameraGeo(ids).then((res) => {
            if (res.code == 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "批量删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            message: "已取消批量删除",
            type: "info",
          });
        });
    },
    //删除图片
    handleImgDelete() {
      this.formCameraGeoNew.imgUrl = "";
      this.showImageUrl = "";
      this.$message({
        message: "删除成功",
        type: "success",
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除该设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delCameraGeo(row.id).then((res) => {
            if (res.code == 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 导出
    handleExport() {
      this.download(
        "camera/geo/export",
        {
          ...this.queryParams,
        },
        `application_${new Date().getTime()}.xlsx`
      );
    },
  },
  computed: {},
  created() {},
  mounted() {
    this.getList();
    this.getCameraList(); //获取摄像头列表
  },
};
</script>

<style lang="scss" scoped>
.mainTable {
  padding: 15px;
  height: 85vh;
  display: flex;
  justify-content: space-around;
  flex-direction: column;
}

.flex-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* 垂直居中对齐 */
}

.btn {
  margin-left: 60px;
  background: linear-gradient(228deg, #ffc04b 0%, #ffa216 100%);
  color: #fff;
  border-radius: 4px 4px 4px 4px;
}

.flex-group-right {
  margin-left: 15px;
}

.rightBtn {
  margin-right: 55px;
  width: 106px;
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%);
  border-radius: 4px 4px 4px 4px;
}

.textStyle {
  margin-left: 5px;
  width: 96px;
  height: 22px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 16px;
  color: #535a6c;
  line-height: 19px;
  text-align: left;
  font-style: normal;
  text-transform: none;
}

::v-deep .el-pager li.active {
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%) !important;
  color: white !important;
}

.search-icon {
  cursor: pointer;
  font-size: 18px;
  vertical-align: middle;
}

::v-deep .el-pagination {
  float: inline-end;
}
</style>
